import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// tsx写法
import vueJsx from '@vitejs/plugin-vue-jsx'
/* 插件支持vue文件name选项写到有setup属性的script标签上，例如：
 *<script setup lang="ts" name="Person">
 */
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
// element-plus自动导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import { viteMockServe } from 'vite-plugin-mock';
import packageJson from './package.json'
// 设置mock
if (process.argv.includes('mock')) {
  // mock环境
  process.env.NODE_ENV = 'mock'
} else if( process.argv.includes('test')) {
  // test环境
  process.env.NODE_ENV = 'test'
}
console.log('process.env.NODE_ENV', process.env.NODE_ENV )

// https://vitejs.dev/config/
export default defineConfig({
  base: `/${packageJson.name}-${packageJson.version}/`,//
  build: {
    outDir: `dist/${packageJson.name}-${packageJson.version}`//打包静态文件输出目录
  },
  define: {
    // __APP_ENV__: JSON.stringify(import.meta.env.MODE)
  },
  plugins: [
    VueSetupExtend(),
    vue(),
    vueJsx(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: ['vue', 'vue-router', 'vuex'],
      dts: resolve('./auto-imports.d.ts')
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      dts: resolve('./components.d.ts')
    }),
    viteMockServe({
      mockPath: "mock", // 解析刚刚user.ts的位置
      enable: 'mock' === 'mock'// 是否启用 mock 功能
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    // 配置省略文件路径的后缀名
    extensions: ['.js', '.json', '.ts', '.vue', 'scss', 'tsx']
  },
  // 配置全局scss文件全局变量
  css: {
    preprocessorOptions: {
      // 没有分号会报错
      scss: {
        additionalData: `@import "@/assets/css/var.scss";`
      }
    }
  },
})
